<template>
  <div class="self">
    <header>
      <i class="iconfont icon-left" @click="$router.replace('/')">
      </i>
      <span>个人设置</span>
    </header>
    <div class="imageProfile">
      <input type="file" id="fileInput" @input="upload_image" />
      <img :src="user.avtar.url||'http://lc-jrl2k7ch.cn-n1.lcfile.com/4e3518cbf943e016e092'" alt="">
      <div class="name">
        <span v-if='!isEdit' @click="isEdit=!isEdit">{{user.username||user.mobilePhoneNumber}}</span>
        <div v-if='isEdit'>
          <input :value="user.username" @input="change_username" type='text'
            :placeholder="user.useruname||user.mobilePhoneNumber"></input>
          <i style="margin-left:10px;" class="iconfont icon-shejiicon" @click="finish"></i>
          <i style="margin-left:10px;" class="iconfont icon-tuichu1" @click="isEdit=!isEdit"></i>
        </div>
      </div>
    </div>
    <div class='lists'>
      <div class="listItem" v-if="isLogin" @click="unlogin">
        <div class="title">登出</div>
        <i class="iconfont icon-right1"></i>
      </div>
      <div class="listItem" v-if="!isLogin">
        <div class="title">登陆</div>
        <i class="iconfont icon-right1"></i>
      </div>
    </div>
  </div>
</template>

<script>
  // @ is an alias to /src
  import { isLogin } from "../../utils";
  import { uploadImage, getUser, updateUser } from './service';
  export default {
    name: "self",
    components: {},
    methods: {
      unlogin() {
        AV.User.logOut();
        this.isLogin = isLogin();
        var self = this;
        setTimeout(() => {
          self.$router.replace("/login");
        }, 1000);
      },
      finish() {
        var self = this
        updateUser(this.user.username).then(res => {
          self.user = getUser().toJSON()
          self.isEdit = false;
        })
      },
      change_username(e) {
        var value = $.trim(e.target.value)
        this.user.username = value
      },
      upload_image(e) {
        var file = e.target.files[0]
        var self = this
        uploadImage(file).then(res => {
          self.user = getUser().toJSON()
          e.target.value = ""
          // self.$router.go(0)
        })


      }
    },
    data() {
      return {
        isEdit: false,
        user: {avtar:{url:""},...getUser().toJSON()},
        isLogin: isLogin()
      };
    }
  };
</script>
<style lang="less" scoped>
  .listItem {
    height: 60px;
    display: flex;
    align-items: center;
    padding-left: 15px;
    padding-right: 15px;
    border-bottom: 1px solid #eee;

    .title {
      flex: 1;
    }
  }

  header {
    padding-left: 15px;
    padding-right: 15px;
    height: 60px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #eee;
    box-shadow: 1px 1px 1px #eee;
  }

  .imageProfile {
    position: relative;
    margin-top: 10px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-self: center;
    align-items: center;

    input[type='file'] {
      position: absolute;
      opacity: 0;
    }

    img {
      width: 110px;
      height: 110px;
      border-radius: 100%;
      border: 2px solid #eee;
      padding: 10px;
    }

    .name {
      margin-top: 10px;
      font-size: 600;
    }
  }
</style>